/**
 * 
 */
package com.webwork.common.dataobject;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

/**
 * 枚举布尔类型
 * 
 */
public enum Bool {

	/**
	 * 
	 * Null.
	 * 
	 * 
	 */
	NULL(-1, "Null"),

	/**
	 * 
	 * True.
	 * 
	 * 
	 */
	TRUE(1, "是"),

	/**
	 * 
	 * False.
	 * 
	 * 
	 */
	FALSE(0, "否"), ;

	private final Integer value;

	private final String cn;

	Bool(Integer v, String c) {
		value = v;
		cn = c;
	}

	/**
	 * 
	 * @return int value ex: return value of FALSE for(Boolean c: Bool.values())
	 *         c.value();
	 */
	public Integer value() {
		return value;
	}

	public Integer getValue() {
		return value;
	}

	public Boolean getBoolean() {
		if (value == null || new Integer(-1).equals(value))
			return null;
		return (new Integer(1).equals(value) ? true : false);
	}

	public String cn() {
		return cn;
	}

	public String getCn() {
		return cn();
	}

	public static Bool[] useableValues() {
		List<Bool> ret = new ArrayList<Bool>();
		for (Bool c: Bool.values()) {
			if (c.value == -1) {
				continue;
			}
			ret.add(c);
		}
		return ret.toArray(new Bool[ret.size()]);
	}

	/**
	 * 
	 * @param v
	 *            create Bool object from value ex: create TRUE
	 *            Bool.fromValue(1);
	 * @return
	 */
	public static Bool fromValue(Integer v) {
		for (Bool c : Bool.values()) {// traversal enum
			if (c.value.equals(v)) {
				return c;
			}
		}
		return Bool.NULL;
	}

	public static Bool fromName(String n) {
		if (StringUtils.isEmpty(n)) {
			return NULL;
		}
		for (Bool c : Bool.values()) {
			if (c.name().equalsIgnoreCase(n)) {
				return c;
			}
		}
		return Bool.NULL;
	}

	public static Bool fromCn(String cn) {
		if (StringUtils.isEmpty(cn)) {
			return NULL;
		}
		for (Bool c : Bool.values()) {
			if (c.cn.equals(cn)) {
				return c;
			}
		}
		return Bool.NULL;
	}

	public static Bool fromBoolean(boolean b) {
		return b ? TRUE : FALSE;
	}

}
